﻿using JinYuan.Helper;
using JinYuan.Models;
using JinYuan.VirtualDataLibrary;
using Language;
using System;
using System.Windows.Forms;

namespace LargeSquareOne
{
    public partial class FrmAlarmLog : MultiLanguageForm
    {
        public FrmAlarmLog()
        {
            InitializeComponent();
        }

        private string CurrentTime
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); }
        }

        /// <summary>
        /// 通用的添加系统日志
        /// </summary>
        /// <param name="isError"></param>
        /// <param name="info"></param>
        public async void AddLog(bool isError, string info)
        {
            if (dgv_Log.InvokeRequired)
            {
                this.dgv_Log.Invoke(new Action<bool, string>(AddLog), isError, info);
            }
            else
            {

                int rowCount = this.dgv_Log.Rows.Add();
                this.dgv_Log.Rows[rowCount].Cells[0].Value = CurrentTime;
                this.dgv_Log.Rows[rowCount].Cells[1].Value = isError ? "错误".Translated() : "信息".Translated();
                this.dgv_Log.Rows[rowCount].Cells[2].Value = info;
                // 设置最新行为活动行 (当前行)
                this.dgv_Log.CurrentCell = this.dgv_Log.Rows[this.dgv_Log.Rows.Count - 1].Cells[0];

                //dgv清理
                if (this.dgv_Log.RowCount > 300)
                {
                    //this.dgv_Log.Rows.RemoveAt(0);
                    this.dgv_Log.Rows.Clear();
                    GC.Collect();
                }
            }

            //存储到数据库
            await CommonMethods.db.AddReturnBoolAsync(new SysLog()
            {
                InsertTime = CurrentTime,
                LogType = "系统日志".Translated(),
                Note = info,
                AlarmType = isError ? "错误".Translated() : "信息".Translated(),
                Operater = CommonMethods.currentAdmin.LoginName
            });
        }

        /// <summary>
        /// 通用的添加操作日志
        /// </summary>
        /// <param name="isError"></param>
        /// <param name="info"></param>
        public async void AddOPLog(bool isError, string info)
        {
            if (this.dgv_OPLog.InvokeRequired)
            {
                this.dgv_OPLog.Invoke(new Action<bool, string>(AddOPLog), isError, info);
            }
            else
            {
                int rowCount = this.dgv_OPLog.Rows.Add();
                this.dgv_OPLog.Rows[rowCount].Cells[0].Value = CurrentTime;
                this.dgv_OPLog.Rows[rowCount].Cells[1].Value = isError ? "错误".Translated() : "信息".Translated();
                this.dgv_OPLog.Rows[rowCount].Cells[2].Value = info;
                // 设置最新行为活动行 (当前行)
                this.dgv_OPLog.CurrentCell = this.dgv_OPLog.Rows[this.dgv_OPLog.Rows.Count - 1].Cells[0];

                //dgv清理
                if (this.dgv_OPLog.RowCount > 300)
                {
                    //this.dgv_OPLog.Rows.RemoveAt(0);
                    this.dgv_OPLog.Rows.Clear();
                    GC.Collect();
                }
            }

            //存储到数据库

            await CommonMethods.db.AddReturnBoolAsync(new SysLog()
            {
                InsertTime = CurrentTime,
                LogType = "操作日志".Translated(),
                Note = info,
                AlarmType = isError ? "错误".Translated() : "信息".Translated(),
                Operater = CommonMethods.currentAdmin.LoginName
            });


        }

        /// <summary>
        /// 通用的添加报警信息
        /// </summary>
        /// <param name="isTrigger">sh</param>
        /// <param name="info"></param>
        public async void AddAlarm(SysLog sysLog)
        {
            if (this.dgv_Alarm.InvokeRequired)
            {
                this.dgv_Alarm.Invoke(new Action<SysLog>(AddAlarm), sysLog);
            }
            else
            {
                int rowCount = this.dgv_Alarm.Rows.Add();
                this.dgv_Alarm.Rows[rowCount].Cells[0].Value = sysLog.InsertTime;
                this.dgv_Alarm.Rows[rowCount].Cells[1].Value = sysLog.AlarmType;
                this.dgv_Alarm.Rows[rowCount].Cells[2].Value = sysLog.Note;
                // 设置最新行为活动行 (当前行)
                this.dgv_Alarm.CurrentCell = this.dgv_Alarm.Rows[this.dgv_Alarm.Rows.Count - 1].Cells[0];
            }

            //dgv清理
            if (this.dgv_Alarm.RowCount > 300)
            {
                //this.dgv_Alarm.Rows.RemoveAt(0);
                this.dgv_Alarm.Rows.Clear();
                GC.Collect();
            }

            //存储到数据库
            await CommonMethods.db.AddReturnBoolAsync(sysLog);

        }

        private void dgv_Log_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridViewHelper.DgvRowPostPaint((DataGridView)sender, e);
        }
        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion

    }
}
